package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.enums.UserAuthAuditStatusDictEnum;
import com.qqtech.qquan.user.model.UserAuth;
import com.qqtech.qquan.user.model.UserAuthQuery;
import com.qqtech.qquan.user.model.UserAuthVo;
import com.qqtech.qquan.user.service.UserAuthService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/auth")
public class UserAuthController extends BaseWebController<UserAuth, UserAuthQuery> {
	@Resource
	private UserAuthService userAuthService;
    
	@Override
	protected BaseService<UserAuth> getBaseService() {
		return userAuthService;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail() {
		Result result = new Result();
		UserAuthVo userAuthVo = userAuthService.getByUserId(getUserId());
		result.put("userAuthVo", userAuthVo);
		return result;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/edit")
	@ResponseBody
	public Result edit(@RequestBody UserAuthVo userAuthVo) {
		Result result = new Result();
		if (userAuthVo == null) {
			result.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(userAuthVo.getRealName())) {
				result.addErr("真实姓名不能为空");
			}
			if (StringUtil.isBlank(userAuthVo.getIdentityNumber())) {
				result.addErr("身份证号码不能为空");
			} else if (!StringUtil.isIdentityNum(userAuthVo.getIdentityNumber())) {
				result.addErr("身份证号码不合法");
			}
			if (StringUtil.isBlank(userAuthVo.getIdentityCardImg())) {
				result.addErr("身份证正面照不能为空");
			}
			if (StringUtil.isBlank(userAuthVo.getIdentityCardBackImg())) {
				result.addErr("身份证反面照不能为空");
			}
			if (StringUtil.isBlank(userAuthVo.getPhotosImg())) {
				result.addErr("个人正面照不能为空");
			}
		}
		if (result.checkForm()) {
			Integer userId = getUserId();
			UserAuth userAuth = userAuthService.getByUserId(userId);
			if (userAuth == null) {
				UserAuth newTemp = new UserAuth();
				newTemp.setUserId(getUserId());
				newTemp.setAuditStatus(UserAuthAuditStatusDictEnum.WAIT.getCode());//未审核
				newTemp.setAuditStatusAt(TimeUtil.now());
				newTemp.setIdentityNumber(userAuthVo.getIdentityNumber());
				newTemp.setRealName(userAuthVo.getRealName());
				newTemp.setNewAt(newTemp.getAuditStatusAt());
				newTemp.setUpdAt(newTemp.getAuditStatusAt());
				newTemp.setIdentityCardImg(userAuthVo.getIdentityCardImg().replace(newTemp.getFileDomain(), ""));
				newTemp.setIdentityCardBackImg(userAuthVo.getIdentityCardBackImg().replace(newTemp.getFileDomain(), ""));
				newTemp.setPhotosImg(userAuthVo.getPhotosImg().replace(newTemp.getFileDomain(), ""));
				newTemp.setBirthPlace("");
				userAuthService.insert(newTemp);
			} else {
				UserAuth updateTemp = new UserAuth();
				updateTemp.setId(userAuth.getId());
				updateTemp.setUserId(getUserId());
				updateTemp.setAuditStatus(UserAuthAuditStatusDictEnum.WAIT.getCode());//未审核
				updateTemp.setAuditStatusAt(TimeUtil.now());
				updateTemp.setIdentityNumber(userAuthVo.getIdentityNumber());
				updateTemp.setRealName(userAuthVo.getRealName());
				updateTemp.setUpdAt(updateTemp.getAuditStatusAt());
				updateTemp.setIdentityCardImg(userAuthVo.getIdentityCardImg().replace(updateTemp.getFileDomain(), ""));
				updateTemp.setIdentityCardBackImg(userAuthVo.getIdentityCardBackImg().replace(updateTemp.getFileDomain(), ""));
				updateTemp.setPhotosImg(userAuthVo.getPhotosImg().replace(updateTemp.getFileDomain(), ""));
				userAuthService.updateById(updateTemp);
			}
		}
		return result;
	}
	
	/**
	 * 判断是否提交认证
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getAuditStatus")
	@ResponseBody
	public Result getAuditStatus() {
		Result result = new Result();
		UserAuthVo userAuthVo = userAuthService.getByUserId(getUserId());
		if (userAuthVo == null) {
			result.put("auditStatus", 0);
			result.put("msg", "未提交身份认证");
		} else if (userAuthVo.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.NOT_THROUGH.getCode()) {
			result.put("auditStatus", userAuthVo.getAuditStatus().intValue());
			result.put("msg", "身份认证审核不通过，请修改");
		} else  if (userAuthVo.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.WAIT.getCode()) {
			result.put("auditStatus", userAuthVo.getAuditStatus().intValue());
			result.put("msg", "身份认证审核中");
		} else  if (userAuthVo.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.THROUGH.getCode()) {
			result.put("auditStatus", userAuthVo.getAuditStatus().intValue());
			result.put("msg", "身份认证审核通过");
		}
		return result;
	}
}
